package com.zzzzzz.oms.orderMonitor;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.zzzzzz.oms.legs.Legs;
import com.zzzzzz.oms.orderDetail.OrderDetail;
import com.zzzzzz.oms.orderHead.OrderHeadDao;
import com.zzzzzz.oms.wave.Wave;
import com.zzzzzz.plugins.jdbctemplate.BaseDao;
import com.zzzzzz.plugins.jdbctemplate.Finder;
import com.zzzzzz.plugins.jdbctemplate.DaoUtils;
import com.zzzzzz.utils.page.Page;
import com.zzzzzz.plugins.shiro.I;

/**
 * @author hing
 * @version 1.0.0
 */
@Repository
public class OrderMonitorDao {
	
	@Resource
	private BaseDao baseDao;
			
	private final static String sql_add = "insert into t_orderMonitor(platformId,cd, clientId, orderId, legsId,shipmentId,constractorId,status,vocation,quantity, mustDt,descr, addDt, addBy) values(:platformId,:cd, :clientId, :orderId, :legsId,:shipmentId,:constractorId,:status,:vocation,:quantity, :mustDt,:descr, :addDt, :addBy)";
	private final static String sql_updStByIds = "update t_orderMonitor set backDt = :backDt,status=:status, updDt = :updDt, updBy = :updBy where id in (:ids)";
	private final static String sql_upd = "update t_orderMonitor set platformId=:platformId, clientId=:clientId, orderId=:orderId, legsId=:legsId,writeDt = :writeDt,receiptDt = :receiptDt,signMan = :signMan,status=:status, descr=:descr, updDt=:updDt, updBy=:updBy where id = :id";
	private final static String sql_findById = "select id, platformId, clientId, orderId, legsId, descr, updDt, addDt, addBy, updBy, st from t_orderMonitor where id = :id";
	private final static String sql_updReasonByIds="update t_orderMonitor set reason = :reason,status=:status, updDt = :updDt, updBy = :updBy where id in (:ids)";
	public Long add(OrderMonitor orderMonitor){
		return baseDao.updateGetLongKey(sql_add, orderMonitor);
	}
	public int updStByIds(List<Long> ids, String backDt, I i){
		Finder finder = new Finder(sql_updStByIds).setParam("ids", ids).setParam("backDt", backDt).setParam("status", "RETURN")
		.setParam("updDt", new Date()).setParam("updBy", i.getId());
		return baseDao.update(finder);
	}	
	
	//延迟单原因
	public int updReasonByIds(List<Long> ids, String reason, I i){
		Finder finder = new Finder(sql_updReasonByIds).setParam("ids", ids).setParam("reason", reason).setParam("status", "NNT")
		.setParam("updDt", new Date()).setParam("updBy", i.getId());
		return baseDao.update(finder);
	}	
	public int updById(OrderMonitor orderMonitor){
		return baseDao.update(sql_upd, orderMonitor);
	}
	public OrderMonitor findById(Long id){
		Finder finder = new Finder(sql_findById).setParam("id", id);
		return baseDao.findOne(finder, OrderMonitor.class);
	}
	
	/*
	 * 批量出入数据
	 */
	public void saveOrderMonitor(List<OrderMonitor> list){
		baseDao.batchUpdate(sql_add, list);
	}
	
	/*
	 * 回单时限控制
	 * 广州3天，其他地区4天
	 */
	public Date timeControl (String translocationName){
		Calendar c = Calendar.getInstance();
		if(translocationName.equals("广州")){
			c.add(Calendar.DAY_OF_MONTH, 3);
		}else{
			c.add(Calendar.DAY_OF_MONTH, 4);
		}
		return c.getTime();
	}
	
	public  List<OrderMonitor> save(List<Legs> list,I i) {
 		List<OrderMonitor> list1=new ArrayList<OrderMonitor>();
 		for(Legs legs:list){
 			OrderMonitor orderMonitor = new OrderMonitor();
 			orderMonitor.setPlatformId(legs.getPlatformId());
 			orderMonitor.setCd(legs.getOrderCd());
 			orderMonitor.setClientId(legs.getClientId());
 			orderMonitor.setOrderId(legs.getOrderId());
 			orderMonitor.setLegsId(legs.getId());
 			orderMonitor.setShipmentId(legs.getShipmentId());
 			orderMonitor.setConstractorId(legs.getConstractorId());
 			orderMonitor.setQuantity(legs.getQuantity());
 			orderMonitor.setStatus("OUTRCP");
 			orderMonitor.setVocation(legs.getVocationType());
 			orderMonitor.setArriveDt(legs.getArrivetime());
 			orderMonitor.setMustDt(timeControl(legs.getCityName()));
 			orderMonitor.setAddBy(i.getId());
 			orderMonitor.setAddDt(new Date());
 			list1.add(orderMonitor);
 		}
		return list1;
	}
	/**
	 * 动态查询
	 */
	public List<OrderMonitor> findListBy(Page page,Map<String, Object> ffMap,I i){
		Finder finder = new Finder("");
		finder.append(" select");
		finder.append("count(1) as tt");
		finder.append(" from t_orderMonitor m inner join t_client c on c.id=m.clientId inner join t_order_head o on o.id=m.orderId "
				+ "inner join t_legs l on l.id=m.legsId inner join t_shipment s on s.id=m.shipmentId inner join sys_dict d on d.descr =m.status "
				+ "inner join t_constractor tc on tc.id=m.constractorId inner join sys_dict dd on dd.descr=m.vocation inner join t_driver td on td.id=s.driverId "
				+ "inner join t_trans_location ft on ft.id=l.ftranslocationId inner join t_trans_location tt on tt.id=l.ttranslocationId");
		finder.append(" where 1=1");
		if(DaoUtils.isNotNull(ffMap.get("clientCd"))){
		finder.append(" and clientCd = :clientCd").setParam("clientCd", ffMap.get("clientCd"));
		}
		if(DaoUtils.isNotNull(ffMap.get("cd"))){
		finder.append(" and cd = :cd").setParam("cd", ffMap.get("cd"));
		}
		if(DaoUtils.isNotNull(ffMap.get("name"))){
		finder.append(" and name like :name").setParam("name", "%" + ffMap.get("name") + "%");
		}
		//finder.append("order by w.id desc");
		if (page != null) {
			Integer total = baseDao.findInt(finder);
			page.setTotal(total);
			finder.limit(page.getStart(), page.getOffset());
		}
		String sql = finder.getSql().replace("count(1) as tt","m.*,c.name as clientName,o.cd as orderCd,l.legs_no as legsCd,l.arrivetime as arriveDt,tc.shortName as constractorName,d.val as statusName,dd.val as vocationName,"
				+ "s.cd as shipmentCd,td.name as driverName,ft.name as freceiverName,tt.name as treceiverName,s.carNo as carNo");
		finder.setSql(sql);
		List<OrderMonitor> list = baseDao.findList(finder, OrderMonitor.class);
				
		return list;
	}
}
